package com.lxyk.controller.monthly;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxyk.domain.StuTest;
import com.lxyk.service.monthly.ClassTestService;
import com.lxyk.service.monthly.StuTestService;
import com.lxyk.utils.PageUtils;
import com.lxyk.utils.StrDateUtils;
import com.lxyk.utils.StuTestCreator;
import com.lxyk.vo.VoClassTest;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.naming.Name;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ExamController
 * @Description TODO
 * @Author FJQ
 * @Date 2022/5/5 15:30
 * @Version 1.0
 **/
@Controller
@RequestMapping("/class_test")
public class ClassTestController {

    @Autowired
    private ClassTestService classTestService;

    @Autowired
    private StuTestService stuTestService;

    @RequestMapping("/query")
    public String query(Integer cid, Model model){
        List<VoClassTest> voClassTests = classTestService.queryByKey(cid);
        System.out.println("voClassTests = " + voClassTests);
        model.addAttribute("voClassTests",voClassTests);
        model.addAttribute("cid",cid);
        return "class-test-query";
    }

    @RequestMapping("/add")
    public String add(Integer cid,Model model){
        model.addAttribute("cid",cid);
        return "class-test-add";
    }


    @RequestMapping("/create")
    @ResponseBody
    public String create(String stage, Date testDate,Integer cid){
        Map map =  new HashMap<>();
        map.put("stage",stage);
        map.put("testDate",testDate);
        map.put("cid",cid);

        List<Integer> ids = classTestService.findIdsByCid(cid);

        List<StuTest> stuTestList = StuTestCreator.createStuTest(ids, map);

        Integer isStuTest = classTestService.findIsStuTest(map);
        if (isStuTest != 0){
            return "-1";
        }

        if(stuTestList.size() == 0 || stuTestList==null ){
            System.out.println("插入失败");
            return "0";
        }
        Integer count = stuTestService.addBatchStuTest(stuTestList);

        if(count == stuTestList.size()){
            System.out.println("全部插入成功");
            return "1";
        }else {
            System.out.println("插入失败");
           return "0";
        }
    }


    @RequestMapping("/findById")
    public String findById(Integer cid,String name,Model model,Integer pageNum,Integer pageSize){
        if (pageNum == null || "".equals(pageNum) || pageNum <=0) {
            pageNum = 1;
        }
        if (pageSize == null || "".equals(pageSize)) {
            pageSize = 10;
        }
        Map map = new HashMap();
        map.put("cid",cid);
        map.put("name",name);
        PageInfo<StuTest> stuTestPageInfo = stuTestService.selectAllById(map, pageNum, pageSize);
        model.addAttribute("stuTestPageInfo",stuTestPageInfo);
        model.addAttribute("cid",cid);
        model.addAttribute("name",name);
        return "class-test-select";
    }

    @RequestMapping("/modifyByStuTest")
    @ResponseBody
    public String modifyByStuTest(Integer id,
                                  Integer result,
                                  Integer makeUp,
                                  Integer pass,
                                  Integer partake){

        StuTest stuTest = new StuTest();
        stuTest.setId(id);
        stuTest.setResult(result);
        stuTest.setMakeUp(makeUp);
        stuTest.setPass(pass);
        stuTest.setPartake(partake);
        Integer count = stuTestService.modifyOne(stuTest);
        if(count == 1){
            return "1";
        }
        return "0";

    }

    @RequestMapping("/queryName")
    public String queryAllByName(Integer cid, String name, Model model,
                                 @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        pageNum = (pageNum-1)*pageSize;
        Map map = new HashMap();
        map.put("name",name);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        PageInfo<StuTest> stuTestPageInfo = stuTestService.queryAllByName(map,pageNum,pageSize);
        model.addAttribute("stuTestPageInfo",stuTestPageInfo);
        model.addAttribute("name",name);
        model.addAttribute("cid",cid);
        return "class-test-select";
    }
}
